package main 

import (
	"fmt"
	"code/project12/customerManage/model"
	"code/project12/customerManage/service"  //引入一个包 【引入一个结构体】
)

type customerView struct{

	key string 
	loop bool

	customerService *service.CustomerService
}


func (this *customerView)delete(){

	id:=-1
	fmt.Println("--------------------删除客户---------------------")
	fmt.Println("请输入客户编号：[-1退出]")

	fmt.Scanln(&id)
	if id==-1{
		return 
	}

	fmt.Println("是否要删除：y/n")

	choice:=""

	fmt.Scanln(&choice)

	if choice=="y"||choice=="Y"{

			if this.customerService.DeleteById(id) {
				fmt.Println("--------------------删除完成---------------------")
			}else{
				fmt.Println("--------------------删除失败，客户编号不存在---------------------")
			}
	}

	
}
//customerView 结构体的成员方法
func (this *customerView)list(){

	customers:=this.customerService.List()

	fmt.Println("--------------------客户列表---------------------")

	fmt.Println("编号\t姓名\t性别\t年龄\t电话\t\t\t邮件")

	for i:=0;i<len(customers);i++{

		fmt.Println(customers[i].GetInfo())
	}

	fmt.Println("--------------------客户列表---------------------\n\n")
}
//customerView 结构体的成员方法
func (this *customerView)add(){
	fmt.Println("--------------------添加客户---------------------")

	fmt.Println("姓名：")
	name:=""
	fmt.Scanln(&name)
	
	fmt.Println("性别：")
	sex:=""
	fmt.Scanln(&sex)

	fmt.Println("年龄：")
	age:=0
	fmt.Scanln(&age)

	fmt.Println("电话：")
	phone:=""
	fmt.Scanln(&phone)

	fmt.Println("邮件：")
	email:=""
	fmt.Scanln(&email)

	customer:=model.NewCustomer2(name,sex,age,phone,email)

	if this.customerService.Add(customer) {
		fmt.Println("添加成功")
	}else{
		fmt.Println("添加失败")
	}
}

func (this *customerView)mainMenu(){

	for{

		fmt.Println("--------------------客户信息管理---------------------")
		fmt.Println("					 1添加客户")
		fmt.Println("					 2修改客户")
		fmt.Println("					 3删除客户")
		fmt.Println("					 4客户列表")
		fmt.Println("					 5退出")
		fmt.Println("					 请选择【1-5】:")

		fmt.Scanln(&this.key)
		switch this.key{
			case "1":
				this.add()
			case "2":
				fmt.Println("修改客户")
			case "3":
				this.delete()
			case "4":
				this.list()
			case "5":
				this.loop=false
			default:
				fmt.Println("输入有误，请重新输入")
		}

		if !this.loop{
			break
		}
	}
	fmt.Println("你退出了软件")
}
func main(){


	customerView:=customerView{
		key:"",
		loop:true,
	}
	customerView.customerService = service.NewCustomerService()
	customerView.mainMenu()
}

